﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Appcode;

namespace MyWeb
{
    public partial class Login1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoginProcessing();

            }
        }

        public void LoginProcessing()
        {
            try
            {
                String cmd = Request.QueryString["do"];
                if (cmd.Equals("logout"))
                {
                    Session.SetLoginUser(null);

                    HttpCookie reg = Request.Cookies["regUser"];
                    if (reg != null)
                    {
                        reg.Expires = DateTime.Now.AddDays(-1d);
                        Response.Cookies.Add(reg);
                    }
                }
            }
            catch (Exception)
            {

            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            LoginToSystem(txtUser.Text, txtPassWord.Text);
            Response.Redirect("Default.aspx");
        }

        public void LoginToSystem(String regAcc, String regPass)
        {
            UsersDAL uDAL = new UsersDAL();
            UserLogin uLog = uDAL.CheckLogin(regAcc, regPass);
            if (uLog != null && uLog.UserName != null)
            {
                //Set session:
                Session.SetLoginUser(uLog);

                //set cookies
                if (rmbchkbox.Checked == true)
                {
                    HttpCookie reg = new HttpCookie("regUser");
                    reg["txtacc"] = regAcc;
                    reg["txtpass"] = regPass;
                    reg.Expires = DateTime.Now.AddDays(1);
                    Response.Cookies.Add(reg);
                }

            }
        }
    }
}